package routes

import (
	"web_app/controllers"
	"web_app/logger"
	"web_app/middleware"
	"web_app/settings"

	"github.com/swaggo/gin-swagger/swaggerFiles"
	"go.uber.org/zap"

	_ "web_app/docs" // 千万不要忘了导入把你上一步生成的docs

	"github.com/gin-gonic/gin"
	gs "github.com/swaggo/gin-swagger"
)

func Setup(cfg *settings.AppConfig) *gin.Engine {
	//初始化Gin框架内置的校验器使用的翻译器
	if err := controllers.InitTrans("zh"); err != nil {
		zap.L().Error("init trans failed ", zap.Error(err))
	}
	//设置gin框架的开发模式
	if cfg.Mode == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode)
	}
	r := gin.New()
	r.Use(logger.GinLogger(), logger.GinRecovery(true))

	r.GET("/swagger/*any", gs.WrapHandler(swaggerFiles.Handler))

	v1 := r.Group("/api/v1")
	//注册路由（用户注册）
	v1.POST("/SignUp", controllers.SignUp)
	//用户登录路由
	v1.POST("/Login", controllers.Login)
	//jwt中间件认证
	v1.Use(middleware.JWTAuthMiddleware())
	{
		//查询社区列表
		v1.GET("/community", controllers.GetCommunityList)
		//查询社区详情
		v1.GET("/communityDetail/:id", controllers.GetCommunityDetail)
		//添加帖子详情数据
		v1.POST("/post", controllers.CreatePost)
		//根据id获取帖子详情
		v1.GET("/post/:id", controllers.GetPostDetailByID)
		//获取帖子列表(分页)
		v1.GET("/post", controllers.GetPostList)
		//帖子列表查询v2（根据时间或分数排序）
		v1.GET("/postV2", controllers.GetPostListInOrder)
		//为帖子添加投票功能
		v1.POST("/postVote", controllers.VoteForPost)
	}
	return r
}
